package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.recyclerview.widget.GridLayoutManager
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentDepartmentBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.KEY_TYPE
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.DepartmentBean
import com.compass.mobile.presenters.DepartmentPresenter
import com.compass.mobile.ui.activity.DepartmentIntroductionActivity
import com.compass.mobile.ui.activity.SearchDoctorAndDepartmentActivity
import com.compass.mobile.ui.adapter.LetterAdapter
import com.compass.mobile.ui.viewModel.InquiryViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 科室列表
 * Date:2024/5/21 20:06
 */
class DepartmentFragment : BaseVBFragment<FragmentDepartmentBinding>() {

    private val inquiryViewModel: InquiryViewModel by viewModels()
    private var letterDept: MutableList<DepartmentBean> = mutableListOf()
    private var searchDept: MutableList<DepartmentBean> = mutableListOf()
    private var mapDept: Map<String, MutableList<DepartmentBean>?>? = null
    private var letter: MutableList<String> = mutableListOf()
    private var comeType: String? = null
    private lateinit var letterAdapter: LetterAdapter//常见症状
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    private var isSearch=false
    override fun initView(view: View, savedInstanceState: Bundle?) {
        comeType = arguments?.getString(KEY_TYPE)
        letterAdapter = LetterAdapter()

        FocusViewUtils.scaleView(mBinding?.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding?.tvNextPage, 1)
        FocusViewUtils.scaleView(mBinding?.edtKeyWords, 0)
        mBinding?.rcyCommon?.apply {
            layoutManager = GridLayoutManager(requireContext(), 4)
            adapter = letterAdapter
        }

        letterAdapter.onItemClickListener = { _: View, p: Int ->
            val item = letterAdapter.getItem(p)
            mBinding?.tvLetter?.text="当前选择分类：$item"
            mapDept?.get(item)?.let {
                searchDept = it
                isSearch=true
                arrayObjectAdapter.clear()
                arrayObjectAdapter.addAll(0,searchDept)
            }
        }
        mBinding?.caseItem?.post {
            // 使用获取到的宽度
            val width = mBinding?.caseItem?.width
            width?.let {
                specialColumn(it)
            }
        }
    }


    override fun initData() {

        inquiryViewModel.departmentListLiveData.observe(this) {
            it?.let { mapList ->
                mapDept = mapList
                for (map in mapList) {
                    letter.add(map.key)
                    map.value?.let { letterDept.addAll(it) }
                }
                letterAdapter.setData(letter)

                arrayObjectAdapter.clear()
                arrayObjectAdapter.addAll(0,letterDept)


            }
            dismissLoading()
        }

    }

    override fun intListener() {
        mBinding?.edtKeyWords?.onClick {
            SearchDoctorAndDepartmentActivity.start(requireContext())
        }
        mBinding?.tvConfirm?.onClick {
            isSearch=false
            arrayObjectAdapter.clear()
            arrayObjectAdapter.addAll(0,letterDept)
            mBinding?.caseItem?.requestFocus()
        }
    }

    private fun getDepartmentList() {
        showLoading()
        when (comeType) {
            //问诊
            "inquiry" -> {
                mBinding?.edtKeyWords?.visible()
                inquiryViewModel.geDeptList()
            }
            //预约
            "booking" -> {
                mBinding?.edtKeyWords?.gone()
                inquiryViewModel.geDeptListTwo()
            }
        }
    }

    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            var item:DepartmentBean?=null
            item = if (isSearch){
                searchDept[position]
            }else{
                letterDept[position]
            }

            comeType?.let {
                DepartmentIntroductionActivity.start(requireContext(), item.id, item.code,it)
            }

        }
    }



    @SuppressLint("RestrictedApi")
    fun specialColumn(width: Int) {
        mBinding?.caseItem?.setNumColumns(2)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        val finalWidth = width - dpToPx(200)
        val departmentPresenter = DepartmentPresenter()
        departmentPresenter.setFinalWidth(finalWidth / 2)
         arrayObjectAdapter = ArrayObjectAdapter(/* presenter = */ departmentPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, letterDept)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
        getDepartmentList()


    }


}

